#include <cstdio>
#include <cstring>

int map[ 301 ][ 301 ], right[ 301 ][ 301 ], down[ 301 ][ 301 ], n, m;

void init( )
{
    int i, j, k;
    scanf("%d%d", &n, &m);
    for ( i = 0; i < n; i++ )
        for ( j = 0; j < m; j++ )
            scanf("%d", &map[ i ][ j ]);
    for ( i = 0; i < n; i++ )
    {
        right[ i ][ 0 ] = map[ i ][ 0 ];
        for ( j = 1; j < m; j++ )
            right[ i ][ j ] = right[ i ][ j - 1 ] + map[ i ][ j ];
    }
    for ( i = 0; i < m; i++ )
    {
        down[ i ][ 0 ] = map[ 0 ][ i ];
        for ( j = 1; j < n; j++ )
            down[ i ][ j ] = down[ i ][ j - 1 ] + map[ j ][ i ];
    }
}

int mabs( int x ) { return ( x < 0 )? -x: x; }

void work( )
{
    int cnt = 0, i, j, k, t;
    for ( i = 0; i < n; i++ )
        for ( j = 0; j < m; j++ )
        {
            t = map[ i ][ j ];
            for ( k = 1; i + k < n && j + k < m; k++ )
            {
                if( j )
                    t += right[ i + k ][ j + k - 1 ] - right[ i + k ][ j - 1 ];
                else
                    t += right[ i + k ][ j + k - 1 ];
                if( i )
                    t += down[ j + k ][ i + k ] - down[ j + k ][ i - 1 ];
                else
                    t += down[ j + k ][ i + k ];
                if ( j )
                {
                    if ( right[ i ][ j + k ] - right[ i ][ j - 1 ] != k + 1 || right[ i + k ][ j + k ] - right[ i + k ][ j - 1 ] != k + 1 )
                        continue;
                }
                else
                {
                    if ( right[ i ][ j + k ] != k + 1 || right[ i + k ][ j + k ] != k + 1 )
                        continue;
                }
                if ( i )
                {
                    if ( down[ j ][ i + k ] - down[ j ][ i - 1 ] != k + 1 || down[ j + k ][ i + k ] - down[ j + k ][ i - 1 ] != k + 1 )
                        continue;
                }
                else
                {
                    if ( down[ j ][ i + k ] != k + 1 || down[ j + k ][ i + k ] != k + 1 )
                        continue;
                }
                if ( k == 1 )
                {
                    //printf("%d %d %d\n", i, j, k);
                    cnt++;
                }
                else
                    if ( mabs( ( t - k * 4 ) * 2 - ( k - 1 ) * ( k - 1 ) ) <= 1 )
                    {
                        //printf("%d %d %d\n", i, j, k);
                        cnt++;
                    }
            }
        }
    printf("%d\n", cnt);
}

int main( )
{
    int t;
    scanf("%d", &t);
    while ( t-- )
    {
        init( );
        work( );
    }
    return 0;
}
